package com.sx.pipeline.ui.message.list

import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.MessageListAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActMessageListBinding
import com.sx.pipeline.repos.http.res.MessageListVo
import com.sx.pipeline.ui.message.MessageVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 消息列表
 */
@Route(path = RoutePath.Message.MESSAGE_LIST)
class MessageListActivity : CYDataBindingBaseActivity<ActMessageListBinding, MessageVM>
    (R.layout.act_message_list), View.OnClickListener {

    @JvmField
    @Autowired(name = "code")
    var code: Int = 0

    @JvmField
    @Autowired(name = "title")
    var title: String? = null

    private lateinit var mAdapter: MessageListAdapter

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.title = title
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        // 获取消息列表
        mViewModel.getNoticeByCode(code)
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.getNoticeByCode(code, mPage)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.getNoticeByCode(code, mPage)
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {

        }
    }

    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 消息列表
        setMessageList()
        // 消息详情
        observe(mViewModel.messageDetailEvent) {
            ARouter.getInstance().build(RoutePath.Message.MESSAGE_DETAIL)
                .withString("title", it?.title)
                .withString("content", it?.content)
                .withString("createTime", it?.createTime)
                .navigation()
        }
    }

    private fun setMessageList() {
        // 消息列表
        observe(mViewModel.messageListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        this, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter = MessageListAdapter(it.records as MutableList<MessageListVo>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->
                        val info = adapter.data[position] as MessageListVo
                        //请求消息详情
                        mViewModel.getNoticeDetail(info.id, info.noticeId)
                    }
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<MessageListVo>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility = View.GONE
            }
        }


    }
}